<script setup>
import { defineEmits, defineProps } from 'vue'
const emit = defineEmits(['selectionChange'])
defineProps({
  columns: {
    type: Array,
    default: () => []
  },
  tableData: {
    type: Array,
    default: () => []
  },
  height: {
    type: Number,
    default: 700
  }
})

const handleSelectionChange = (e) => {
  emit(
    'selectionChange',
    e.map((item) => item.id)
  )
}
</script>
<template>
  <div>
    <el-card style="max-width: 100%">
      <template #header>
        <div>
          <slot name="header"></slot>
        </div>
      </template>

      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          v-for="(item, index) in columns"
          :key="index"
          :prop="item.prop"
          :type="item.type"
          :label="item.label"
          :width="item.width"
          :formatter="item.formatter"
          :fixed="item.fixed"
        >
          <template #default="scope">
            <!-- 文本 -->
            <span
              v-if="item.type === 'text'"
              style="
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
              "
              :style="{ lineClamp: item.lineClamp || 3 }"
            >
              {{ scope.row[item.prop] }}
            </span>
            <!-- 价格 -->
            <span
              v-if="item.type === 'price'"
              style="
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
              "
              :style="{ lineClamp: item.lineClamp || 3 }"
            >
              {{ scope.row[item.prop] }}￥
            </span>
            <!-- 图片 -->
            <span v-else-if="item.type === 'img'">
              <el-image
                :src="scope.row[item.prop]"
                :style="{
                  width: item.config.width,
                  height: item.config.height
                }"
              ></el-image>
            </span>
            <!-- 时间 -->
            <span v-else-if="item.type === 'date'">
              {{ scope.row[item.prop]?.split(' ')[0] }}
            </span>
            <!-- 枚举 -->
            <span
              v-else-if="item.type === 'enum'"
              :style="{
                color: item.enum.find((e) => e.value === scope.row[item.prop])
                  ?.color
              }"
            >
              {{
                item.enum.find((e) => e.value === scope.row[item.prop])?.label
              }}
            </span>
            <!-- 连接 -->
            <span v-else-if="item.type === 'link'">
              <a
                :href="scope.row[item.prop]"
                style="
                  overflow: hidden;
                  text-overflow: ellipsis;
                  display: -webkit-box;
                  -webkit-box-orient: vertical;
                  color: #60a0f8;
                "
                :style="{ lineClamp: item.lineClamp || 3 }"
                >{{ scope.row[item.prop] }}</a
              >
            </span>
            <!-- 数组 -->
            <span v-else-if="item.type === 'tags'">
              <span v-for="(tab, index) in scope.row[item.prop]" :key="index">
                <el-tag :key="tab" style="margin-right: 0.5rem" type="primary">
                  {{ tab }}
                </el-tag>
              </span>
            </span>
            <!-- 操作 -->
            <span v-else-if="item.type === 'operate'">
              <slot name="operate" :row="scope.row"></slot>
            </span>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <div class="pagination-box">
          <slot name="pagination"></slot>
        </div>
      </template>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.pagination-box {
  display: flex;
  justify-content: end;
}
</style>
